﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using RemoteTestRunner.BusinessLogic.Properties;

namespace RemoteTestRunner.BusinessLogic.ViewModels
{
    public class OptionsViewModel : INotifyPropertyChanged, IRtrSettings
    {

        #region DataPath (INotifyPropertyChanged Property)
        public string DataPath
        {
            get { return Settings.Default.DataPath; }
            set
            {
                if (Settings.Default.DataPath != value)
                {
                    Settings.Default.DataPath = value;
                    OnPropertyChanged("DataPath");
                }
            }
        }
        #endregion

        #region MSTestPath (INotifyPropertyChanged Property)
        public string MSTestPath
        {
            get { return Settings.Default.MSTestPath; }
            set
            {
                if (Settings.Default.MSTestPath != value)
                {
                    Settings.Default.MSTestPath = value;
                    OnPropertyChanged("MSTestPath");
                }
            }
        }
        #endregion

        #region WorkingFolder (INotifyPropertyChanged Property)
        public string WorkingFolder
        {
            get { return Settings.Default.WorkingFolder; }
            set
            {
                if (Settings.Default.WorkingFolder != value)
                {
                    Settings.Default.WorkingFolder = value;
                    OnPropertyChanged("WorkingFolder");
                }
            }
        }
        #endregion

        #region TestTimeoutSeconds (INotifyPropertyChanged Property)
        public int TestTimeoutSeconds
        {
            get { return Settings.Default.TestTimeoutSeconds; }
            set
            {
                if (Settings.Default.TestTimeoutSeconds != value)
                {
                    Settings.Default.TestTimeoutSeconds = value;
                    OnPropertyChanged("TestTimeoutSeconds");
                }
            }
        }
        #endregion

        #region TestRunMode (INotifyPropertyChanged Property)
        public RunMode TestRunMode
        {
            get
            {
                if (UseRunnerService)
                {
                    return RunMode.Server;
                }
                else
                {
                    return RunMode.Local;
                }
            }
            set
            {
                if (value == RunMode.Server)
                {
                    UseRunnerService = true;
                    OnPropertyChanged("TestRunMode");
                }
                else
                {
                    UseRunnerService = false;
                    OnPropertyChanged("TestRunMode");
                }
            }
        }
        #endregion

        public bool UseRunnerService
        {
            get
            {
                return Settings.Default.UseRunnerService;
            }
            set
            {
                if (value != Settings.Default.UseRunnerService)
                {
                    Settings.Default.UseRunnerService = value;
                    OnPropertyChanged("UseRunnerService");
                }
            }
        }

        #region Url (INotifyPropertyChanged Property)
        public string RunnerServiceUrl
        {
            get { return Settings.Default.RunnerServiceUrl; }
            set
            {
                if (Settings.Default.RunnerServiceUrl != value)
                {
                    Settings.Default.RunnerServiceUrl = value;
                    OnPropertyChanged("Url");
                }
            }
        }
        #endregion

        #region FilterTests (INotifyPropertyChanged Property)
        public bool FilterTests
        {
            get { return Settings.Default.FilterTests; }
            set
            {
                if (Settings.Default.FilterTests != value)
                {
                    Settings.Default.FilterTests = value;
                    OnPropertyChanged("FilterTests");
                }
            }
        }
        #endregion

        public static event EventHandler SettingsUpdated;

        public static void Save()
        {
            Settings.Default.DefaultSettings = false;
            Settings.Default.Save();
            if (SettingsUpdated != null)
            {
                SettingsUpdated(null, EventArgs.Empty);
            }
        }

        #region INotifyPropertyChanged values

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }

    public enum RunMode
    {
        Local,
        Server
    }
}
